<?php

require_once 'comun.php';

class BasicFunctions
{
    /**
     * @param string $str
     */
    public function printLine($str)
    {
        echo $str.PHP_EOL;
    }
    
    public function printSeparator()
    {
        echo '------------------------------------------------'.PHP_EOL;
    }    
    
    public function newLine()
    {
        echo PHP_EOL;
    }

    /**
     * @param string $str
     */
    public function printStr($str)
    {
        echo $str;
    }
}

class AdvancedFunctions
{
    public function printDate()
    {
        echo 'Date: '.$this->getDate().PHP_EOL;
    }    
    
    /**
     * @return string
     */
    protected function getDate()
    {
        return date('Y-m-d H:i:s');
    }    

    /**
     * @param string $tpl
     * @param mixed $vars
     */
    public function printTpl($tpl, $vars)
    {
        $vars = func_get_args();
        $tpl = array_shift($vars);    
        echo vsprintf($tpl, $vars).PHP_EOL;
    }  
    
    /**
     * Override BasicFunctions::printStr
     * @param string $str
     * @param boolean $upperCase
     */
    public function printStr($str, $upperCase = false)
    {
        echo ($upperCase) ? strtoupper($str) : $str;
    }    
}

/**
 * @method void printLine(string $str)
 * @method void printSeparator(string $str)
 * @method void printStr(string $str)
 * @method void newLine()
 */
class Printer
{
    // INI: Mixins vars
    protected $implementMixins = array('BasicFunctions');
    protected $mixinsMethods = array();
    protected $mixinsInstances = array();
    // END: Mixins vars
    
    protected $name;
    
    public function __construct($name)
    {
        $this->doMixins();
        $this->name = $name;
    }    
    
    public function switchOn()
    {
        $this->printSeparator();
        $this->printLine($this->name.' switched ON.');
    }  
    
    public function switchOff()
    {
        $this->printLine($this->name.' switched OFF.');
        $this->printSeparator();
    }   
    
    /**
     * @param string $method
     * @param array $params
     */
    public function __call($method, $params = array())
    {       
        $mixin = $this->getMixinByMethodName($method);
        if (!is_null($mixin))
        {
            call_user_method_array($method, $mixin, $params);
        }
        else
        {
            trigger_error('Call to undefined function "'.$method.'"', E_USER_ERROR);
        }   
    }    
    
    protected function doMixins()
    {
        if (is_array($this->implementMixins) && count($this->implementMixins)>0)
        {
            foreach ($this->implementMixins as $className)
            {
                $methods = get_class_methods($className);
                if (is_array($methods) && count($methods)>0)
                {
                    $this->mixinsMethods[$className] = $methods;
                    $this->mixins[$className] = new $className();
                }    
            } 
        } 
        // print_r($this->mixinsMethods);
    } 
    
    /**
     * @param string $method
     * @return mixed | null
     */
    protected function getMixinByMethodName($method)
    {
        $mixin = null;
        foreach ($this->mixinsMethods as $class=>$methodNames)
        {
            if (is_array($methodNames) && in_array($method, $methodNames))
            {
                if (isset($this->mixins[$class]))
                {
                    $mixin = $this->mixins[$class];
                }    
                break;
            }    
        }
        return $mixin;
    }    
}


class BasicPrinter extends Printer
{    
    /**
     * @param string $name
     * @return BasicPrinter
     */
    public static function factory($name)
    {
        return new self($name);
    }    
    
    /**
     * Override mixin method of parent class
     * @param string $str
     */
    public function printLine($str)
    {
        parent::printLine($this->name.' :: '.$str);
    }    
}

/**
 * @method void printStr(string $str, boolean $upperCase)
 * @method void printTpl(string $tpl, mixed $vars)
 * @method void printDate()
 */
class AdvancedPrinter extends Printer
{
    protected $implementMixins = array('AdvancedFunctions', 'BasicFunctions');
    
    /**
     * @param string $name
     * @return AdvancedPrinter
     */
    public static function factory($name)
    {
        return new self($name);
    }   
}

class WrongPrinter extends Printer
{
    protected $implementMixins = array(); // Bad code, no mixins => error!!
    
    /**
     * @param string $name
     * @return FakePrinter
     */
    public static function factory($name)
    {
        return new self($name);
    } 
}

// Basic printer
$p1 = BasicPrinter::factory('P1');
$p1->switchOn();
$p1->printLine('Hell world!!');
$p1->printStr('Testing mixins priority...');
$p1->newLine();
$p1->switchOff();

// Advenced printer
$p2 = AdvancedPrinter::factory('P2');
$p2->switchOn();
$p2->printTpl('Hello %s!!', 'world');
$p2->printStr('Testing mixins priority...', true);
$p2->newLine();
$p2->printDate();
$p2->switchOff();

// Non working printer (bad implementation)
$p3 = WrongPrinter::factory('P3');
$p3->switchOn(); // Fatal error!!



die();
// Pruebas con los loggers...
My_logger::getInstance()
    ->setSystem('TEST-SCRIPT')
    ->addToEnvironment('UID', 12345)
    ->setVerbose(true);

My_Logger::getInstance()->info('HOLA %s %s', 'MUNDO', Zend_Date::now());
My_Logger::i()->stepIn('Step-1')->success('Dentro de step 1');
My_Logger::getInstance()->stepOut();
My_Logger::getInstance()->reset()->info('FIN DEL SCRIPT');



$params = array(
  'host' => 'imap.googlemail.com',
  'user' => 'miguel@fixeno.eu',
  'password' => 'dspass12',
  'port' => '993',
  'ssl' => 'SSL'
);
$storage = new Zend_Mail_Storage_Imap($params);
$numberOfMessages = $storage->countMessages();

if ($numberOfMessages > 0)
{  
    foreach ($storage as $msg)
    {
        /* @var $msg Zend_Mail_Message */
        $subject = $msg->getHeader('subject');
        if (true == $msg->isMultipart())
        {
            $parts = $msg->countParts();
            Zend_Debug::dump($parts);
            for($i = 1; $i<=$parts; $i++)
            {
                $part = $msg->getPart($i);
                Zend_Debug::dump($part->getHeaders());
                //$content = $part->getContent();
                Zend_Debug::dump($content, 'CONTENT-'.$i);
                echo PHP_EOL.'---------------------'.PHP_EOL;
            }
        }
//        $body = $msg->getContent();
//        Zend_Debug::dump($body);
//        Zend_Debug::dump($msg->getFlags());
    }
}    
 

